package com.hzjq.fxlib.presenter

import android.view.KeyEvent
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.hzjq.core.bean.ErrorCapEntity
import com.hzjq.fxlib.R
import com.hzjq.fxlib.ui.ErrorCapActivity
import com.wxl.common.util.Toll
import kotlinx.android.synthetic.main.layout_register_scan.view.*

class RegisterScanPresenter : AbsViewPresenter {

    private var isEnd = false

    private val errorCaps = arrayListOf<ErrorCapEntity>()

    constructor(rootView: ViewGroup) : super(rootView)

    override fun onContentViewId(): Int {
        return R.layout.layout_register_scan
    }

    override fun onResume() {

    }

    override fun showAction(action: String) {
        rootView.registerCapScanState.text = action
        rootView.registerCapScanState.setTextColor(ContextCompat.getColor(getContext(),android.R.color.black))
    }

    override fun notifyProgressBar(action: String, p: Int) {
        rootView.registerCapScanState.text = action
        rootView.registerCapScanState.setTextColor(ContextCompat.getColor(getContext(),android.R.color.black))
    }

    fun notifyScanCountChanged(scanCount:Int,okCount:Int,meetCount:Int,missCount:Int,isEnd:Boolean){
        rootView.registerCapScanCount.text = "$scanCount"
        rootView.registerCapScanOkCount.text = "$okCount"
        rootView.registerCapScanMissCount.text = "$missCount"
        rootView.registerCapMeetCount.text = "$meetCount"
        this.isEnd = isEnd
    }

    fun setErrorCap(errorCaps : MutableList<ErrorCapEntity>){
        this.errorCaps.clear()
        this.errorCaps.addAll(errorCaps)
        rootView.registerCapScanState.text = if(errorCaps.isEmpty()) "提示:组网出错，请检测雷管状态后重试" else "提示：组网出错，按F1查看详情"
        rootView.registerCapScanState.setTextColor(ContextCompat.getColor(getContext(),android.R.color.holo_red_dark))
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(isEnd){
            if(keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5){
                if(errorCaps.isNotEmpty()){
                    ErrorCapActivity.start(getContext(),errorCaps)
                    return true
                }
            }
        } else {
            Toll.toll("正在检测中...")
        }
        return super.onKeyDown(keyCode, event)
    }
}